package com.ivan.stu.notetool.network.api;

import android.text.TextUtils;
import android.util.Log;


import com.ivan.stu.notetool.network.intercepter.FastJsonConverterFactory;
import com.ivan.stu.notetool.network.intercepter.LoggingInterceptor;
import com.ivan.stu.notetool.network.intercepter.MyRequestHeaderInterceptory;
import com.ivan.stu.notetool.network.intercepter.RequestEncryptInterceptor;
import com.ivan.stu.notetool.network.intercepter.RequestHeaderInterceptor;
import com.ivan.stu.notetool.utils.ConstrantManager;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
//import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Created by IVAN on 2018/7/11.
 */

public class SafeApiManager {

    private static OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient
            .Builder()
            //有网时使用的拦截器
//            .addNetworkInterceptor(new StethoInterceptor())//调试用
            .addInterceptor(getHttpLoggingInterceptor())
            .addInterceptor(new RequestHeaderInterceptor())
            .addInterceptor(new RequestEncryptInterceptor())
            .addInterceptor(new MyRequestHeaderInterceptory())

            //所有情况下使用的拦截器
            // .addInterceptor(new UrlInterceptor())
            /**
             * default timeout
             * connectTimeout = 10_000;
             * readTimeout = 10_000;
             * writeTimeout = 10_000;
             */

            .connectTimeout(10, TimeUnit.SECONDS) //原来是25秒
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS);

    static public HttpLoggingInterceptor getHttpLoggingInterceptor(){
        //日志显示级别
        HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (TextUtils.isEmpty(message)) return;
                Log.d("aaaa","收到响应: " + message);
                if (message.startsWith("{") || message.startsWith("[")) {
//                    LogUtils.d("aaaa","收到响应: " + message);
                }
            }
        });
        loggingInterceptor.setLevel(level);
        return loggingInterceptor;
    }

    private static Retrofit.Builder getBuilder() {
        if (ConstrantManager.IS_DEBUG){
            okHttpClientBuilder.addInterceptor(new LoggingInterceptor());
        }
        return new Retrofit.Builder()
                .client(okHttpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
    }

    /**
     * 用于测试的接口
     * 0:基础平台
     * 1：大数据
     * 2：学习小管家
     * 3: 笔记
     * @return
     */
    public static TestApi getTestApi(int flag) {

            return getBuilder().baseUrl(ConstrantManager.NOTE_URL).build().create(TestApi.class);


    }

}
